package com.taotao.controller;

import com.taotao.utile.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Title: </p>
 * <p>Company: </p>
 *
 * @Author: smbe
 * @Date: Create in 20:41 2017/9/9
 * @Description:使用fastDFS完成图片上传
 */
@Controller
public class ImageUploadController {
    @Value("${IMAGE_SERVER_URL}")
    String IMAGE_SERVER_URL;
    @RequestMapping("/pic/upload")
    @ResponseBody
    public Map uploadImage(MultipartFile uploadFile){

        try {
            //得到文件的原名
            String originalName = uploadFile.getOriginalFilename();
            //得到文件的后缀名,不需要".";
            String subName = originalName.substring(originalName.lastIndexOf(".")+1);
            //创建fastDFS客户端对象
            FastDFSClient client = new FastDFSClient("classpath:resource/fast_cli.conf");
            //进行文件上传
            String path = client.uploadFile(uploadFile.getBytes(), subName);

            //拼接完整的url
            String URL = IMAGE_SERVER_URL+path;
            Map result = new HashMap();
            System.out.println(URL);
            result.put("error",0);
            result.put("url",URL);
            return  result;

        } catch (Exception e) {
            e.printStackTrace();
            Map result = new HashMap();
            result.put("error",0);
            result.put("message","图片上传失败");
            return  result;

        }


    }


}
